#include "lbvh_structure.h"
#include <utility\cuda_utils.h>
#include <assert.h>

namespace gc {
	void LBvhLeaves::malloc(uint size) {
		checkCudaErrors(GPUMalloc(parent, sizeof(int)*size));
		checkCudaErrors(GPUMalloc(primIdx, sizeof(int)*size));
		checkCudaErrors(GPUMalloc(segLen, sizeof(int)*size));
		checkCudaErrors(GPUMalloc(segOffset, sizeof(int)*size));
		checkCudaErrors(GPUMalloc(escape, sizeof(int)*size));
		checkCudaErrors(GPUMalloc(morton, sizeof(uint)*size));
		bv.malloc(size);
	}

	void LBvhLeaves::free() {
		checkCudaErrors(cudaFree(parent));
		checkCudaErrors(cudaFree(primIdx));
		checkCudaErrors(cudaFree(segLen));
		checkCudaErrors(cudaFree(segOffset));
		checkCudaErrors(cudaFree(escape));
		checkCudaErrors(cudaFree(morton));
		bv.free();
	}

	void LBvhInteriors::malloc(uint size) {
		checkCudaErrors(GPUMalloc(parent, sizeof(int)*size));
		checkCudaErrors(GPUMalloc(lc, sizeof(int)*size));
		checkCudaErrors(GPUMalloc(rc, sizeof(int)*size));
		checkCudaErrors(GPUMalloc(rangeL, sizeof(int)*size));
		checkCudaErrors(GPUMalloc(rangeR, sizeof(int)*size));
		//checkCudaErrors(cudaMemset(rangeR, 0, sizeof(int)*size));
		checkCudaErrors(GPUMalloc(escape, sizeof(int)*size));
		bv.malloc(size);
		bvR.malloc(size);
	}
	
	void LBvhInteriors::free() {
		checkCudaErrors(cudaFree(parent));
		checkCudaErrors(cudaFree(lc));
		checkCudaErrors(cudaFree(rc));
		checkCudaErrors(cudaFree(rangeL));
		checkCudaErrors(cudaFree(rangeR));
		checkCudaErrors(cudaFree(escape));
		bv.free();
		bvR.free();
	}
}